﻿
import YouHP.Graphics.*;

class TreatmentChart extends PatientChartBase
{
    var mClip : MovieClip; 
    var mHeight : Number;

    var mTime1 : Array;
    var mTime2 : Array;
    var mValue1 : Array;
    var mValue2 : Array;
    var mValue1Range : Array;
    var mValue1Color : Array;
    var mRange2 : Array;
    var mName1 : String;
    var mName2 : String;
    var mUnit1 : String;
    var mUnit2 : String;
    
    var mDataTop : Number;
    var mDataBottom : Number;
    var mY1Grid : Array;
    var mY2Grid : Array;
    var mDataColor : Number = 0x1B52AD;
    
    var mData : Object;
    var mLeft;
    var mRight;
    
    var mOption : Array;
       
    function TreatmentChart(lv : Object)
    {
        mClip = _root.createEmptyMovieClip("TreatmentChart", _root.getNextHighestDepth());
        mData = lv;
        
        if(mData["GroupCount"] == undefined) 
        {
            _root.ChartSucc = false;
            return;
        }
        
        PraseBase(lv);
        
        mHeight = 300;
        
        Setup();
        Draw();
    }
    
    public function Draw()
    {
        //DrawLegend();
        DrawData();        
    }
    
    public function Setup()
    {
        
        mLeft = 10;
        mRight = mWidth - 10;
        mDataTop = 20;
        mDataBottom = mHeight - 20;     
        
        
        
        mOption = new Array();
        mOption.push(["治疗本病",0xFE0000]);
        mOption.push(["治疗本病或其他疾病",0xFE9900]);
        mOption.push(["保健",0x339966]);
        mOption.push(["其他或不清楚",0xFFFFFF]);
        
       
    }
    
    
    public function DrawData()
    {
        
        var mc : MovieClip = mClip;
        var gs:Graphics = new Graphics(mClip);
        
        var y:Number;
        
        var gridpen = new Pen(1.0,0xcccccc,100);
        var borderpen = new Pen(1.0,0x000000,100);
       

        var y = 20;
        var gn : Number = Number(mData["GroupCount"]);
        if(gn == undefined){
             _root.ChartOk = false;
             return;
        }
        for(var i=1; i <= gn; i++)
        {
            
            var gArray : Array = mData["Group"+i].split(";");
            var gname = gArray[0];
            gs.DrawDashLine(gridpen,mLeft,y,mDataRight,y,1,1);            
            var label:TextField = mc.createTextField("gname" + i ,mc.getNextHighestDepth(),  mLeft, y, 0, 0);
            var myformat = new TextFormat();
            myformat.color = 0xff0000;
            myformat.align = "left";
            myformat.size  = 18;
            label.setTextFormat(myformat);
            label.html = true;
            label.autoSize = "left";
            label.htmlText = gname;
            y += 30;
            var vcnt : Number = Number(gArray[1]);
            if(vcnt == undefined){
                _root.ChartOk == false;
                return;
            }
            for(var j=1; j <= vcnt; j++)
            {
                var varArray : Array = mData["Var_" + i + "_" + j].split(";");
                var vname = varArray[0];
                var valueCnt = Number(varArray[1]);
                if(valueCnt == undefined){
                    _root.ChartOk = false;
                    return;
                }
                
                gs.DrawDashLine(gridpen,mDataLeft,y,mDataRight,y,1,1);            
                var label:TextField = mc.createTextField("vname_" + i + "_" + j, mc.getNextHighestDepth(),mDataLeft-3, y-9, 0, 0);
                var myformat = new TextFormat();
                myformat.color = 0xff0000;
                myformat.align = "right";
                label.setTextFormat(myformat);
                label.html = true;
                label.autoSize = "right";
                label.htmlText = vname;

                                
                var n = valueCnt;
                var lastx = -1;
                var lastv = -1;
                for(var k=1; k <= n; k++)
                {
                    var dataArray : Array = mData["Data_" + i + "_" + j + "_" + k].split(";");
                    var dt : Date = Utils.StrToDate(dataArray[0]);
                    var dt2 : Date = Utils.StrToDate(dataArray[1]);
                    var v = Number(dataArray[2]);
                    var dosage = Number(dataArray[3]);
                    var unit = dataArray[4];
                    trace(k);
                    var x = this.GetX(dt);
                    var x2 = this.GetX(dt2);
                    
                    
                    var varmc = this.CreateMovieClip("Data_" + i + "_" + j + "_" + k);
					trace("dt : " + dt);
                    var year = dt.getFullYear();
                    var month = dt.getMonth();
                    var day = dt.getDate();
                    var msg : String = "<p>从 <b>" + year + "年" + month + "月" + day + "日 </b></p>";
					trace("dt2 : " + dt2);
                    var year = dt2.getFullYear();
                    var month = dt2.getMonth();
                    var day = dt2.getDate();
                    msg += "<p>到 <b>" + year + "年" + month + "月" + day + "日</b></p>";
                    msg += "<p>" + vname + ": " + dosage + " " + unit + "</p>"; 
                    varmc.ToolTip(msg);
                    trace("x=" + x + "   x2 = " + x2); 
                    var vargs:Graphics = new Graphics(varmc);
                    var brush : Brush = new SolidBrush(mOption[v][1],100);
                    vargs.FillRect(brush,x,y-5,2,10);
                    vargs.DrawRect(borderpen,x,y-5,2,10);
                    vargs.FillRect(brush,x+2,y-2,x2-x,4);
                    vargs.DrawRect(borderpen,x+2,y-2,x2-x,4);
                    
                    
                    lastx = x;
                }
                y += 30;
            }
/*			
            if(i == 1 && vcnt < 4)
            {
                 y += 30*(4-vcnt);
            }
*/			
        } 
        
		mDataBottom = y;
        // 画x坐标
        var pen = new Pen(1.0,0xcccccc,100);
        gs.DrawLine(pen,mStartX,mDataTop - 10 ,mStartX,mDataBottom + 10);                
        gs.DrawLine(pen,mEndX,mDataTop - 10 ,mEndX, mDataBottom + 10);        
         gs.DrawLine(gridpen,mDataRight,mDataTop,mDataRight,mDataBottom);
		 
		var year1 = mStartDate.getFullYear();
		trace("year1 : " + year1);
		trace("start date" + mStartDate);		
		var year2 = mUpdateDate.getFullYear();
		trace("year2 : " + year2);
		if(year1 != undefined && year2 != undefined)
		{
		    if(year1 +1 < year2)
		    { 
		        for(var i=year1+1; i <= year2; i++)
		        {
			        var dt : Date = new Date(i,1,1);
			        var x : Number = GetX(dt);
                    gs.DrawDashLine(pen,x,mDataTop,x,mDataBottom,1,1);
   				    var label:TextField = mc.createTextField("gridx" + i ,mc.getNextHighestDepth(), 
   				        x,mDataBottom + 10, 0, 0);
                    var myformat = new TextFormat();
        		    myformat.color = 0xff0000;
		            myformat.align = "center";
		            label.setTextFormat(myformat);
		            
        		    label.html = true;
		            label.autoSize = "center";
		            label.htmlText = "" + i + ".01";
		        }
		    }else
		    {
		        var month1 = mStartDate.getMonth();
		        var month2 = mUpdateDate.getMonth();
		        var cnt = (year2 - year1)*12 + (month2 - month1);

				var year = year1 ;
				var month = month1 + 1;
				for(var i=0; i < cnt; i++)
				{
					if(month > 12 ) {
						month -= 12;
						year += 1;
					}
					var dt : Date = new Date(year,month);
					var x : Number = GetX(dt);
				    trace(" dt = " + dt + "   x=" + x);
                    gs.DrawDashLine(pen,x,mDataTop,x,mDataBottom,1.5,1.5);
                    trace("draw dash line " + i);
               		var label:TextField = mc.createTextField("gridx" + i ,mc.getNextHighestDepth(), 
               		  x, mDataBottom + 10, 0, 0);
                    var myformat = new TextFormat();
        		    myformat.color = 0xff0000;
		            myformat.align = "center";
		            label.setTextFormat(myformat);
		            
        		    label.html = true;
		            label.autoSize = "center";
		            label.htmlText = "" + year + "." + month;
		            month = month + 1;
				}
		    }
		}
		
		var pen = new Pen(2.0,0x000000,100);
        gs.DrawLine(pen,mDataLeft,mDataBottom,mDataRight,mDataBottom);
        gs.DrawLine(pen,mDataLeft,mDataBottom,mDataLeft,mDataTop);
		
		var swfheight = mDataBottom;
		this.ResizeChart(0,swfheight + 48);
    }
    
    public function DrawLegend()
    {
        var mc : MovieClip = mClip;
        var gs:Graphics = new Graphics(mClip);
        var mLegendLeft = 20;
        var mLegendBottom = mHeight - 60;
        var pen = new Pen(1.0,0x000000,100);
        var startx = mLegendLeft;
        var n = mOption.length;
        var starty = 65;
        var y;
        for(var i=0; i < n; i++)
        {
            y = starty + i * 20;
            
            var name = mOption[i][0];
            var color = mOption[i][1];
            
            var brush = new SolidBrush(color,100);
            gs.FillRect(brush,startx + 10,y,15,15);
            gs.DrawRect(pen,startx + 10,y,15,15);
            
            var label:TextField = mc.createTextField("option_" + i ,mc.getNextHighestDepth(),
              startx + 30,y-3, 0, 0);
            var myformat = new TextFormat();
        	myformat.color = 0xff0000;
		    myformat.align = "left";
		    label.setTextFormat(myformat);
		            
        	label.html = true;
		    label.autoSize = "left";
		    label.htmlText = name;
        }
        gs.DrawRect(pen,startx,starty-10,150,n*20 + 15);
        
        var startx = 20;
        var starty = 30;
        
      
        
    }
    

    public function CreateMovieClip(name:String) : MovieClip
    {
        var m = _root.createEmptyMovieClip(name, _root.getNextHighestDepth());
        return m;
    }
}